<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<div class="row">
    <div class="col-md-12">
        <ul id="tree_department" class="ztree"></ul>
    </div>
</div>

<script>

    var treeObj;

    $(function(){

        init_tree_department();

    });

    function init_tree_department() {

        var setting = {
            async: {
                enable: true,
                url:'<%=path%>/main/department/tree',
                autoParam: ['id'],
                type: 'get',
                dataType: 'json'
            },
            callback: {
                onAsyncSuccess: zTreeOnAsyncSuccess,
                onCheck: zTreeOnCheck
            },
            check: {
                enable: true,
                <c:if test="${multi=='y'}">
                chkStyle: 'checkbox',
                </c:if>
                <c:if test="${multi=='n'}">
                chkStyle: 'radio',
                radioType: "all",
                </c:if>
                chkboxType:  { 'Y' : '', 'N' : '' }
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',
                    pIdKey: 'pId',
                    rootPId: ''
                }
            },
            view: {
                autoCancelSelected: false,
                dblClickExpand: false,
                selectedMulti: true
            }
        };

        treeObj = $.fn.zTree.init($('#tree_department'), setting);

    }

    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        if (treeNode) {
            <c:forEach var="item" items="${deptIds}" varStatus="status">
                var node = zTree.getNodeByParam("id", "${item}");
                zTree.checkNode(node,true,false);
            </c:forEach>
            /*
            if (treeNode.id === '0') {
                var nodes = treeNode.children;
                zTree.checkNode(nodes[0],true,false);

            }*/
        } else {
            var rootNode = zTree.getNodes()[0];
            if (rootNode.isParent) {
                zTree.expandNode(rootNode, true, true, true);
            }
        }
    }

    function zTreeOnCheck(event, treeId, treeNode){
//        console.log(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
    }

    function fn_dialog_departments_select(callback) {
//        var treeObj = $.fn.zTree.getZTreeObj('tree_department');
        var nodes = treeObj.getCheckedNodes();
        console.debug(nodes);
        /*
        if(nodes.length < 1){
            __toastr_warning('请至少选择一个部门');
            callback();
            return;
        }*/
        var deptNames = "";
        var deptIds = "";
        for(var i=0; i<nodes.length; i++){
            deptNames += nodes[i].name + ",";
            deptIds += nodes[i].id + ",";
        }
        deptNames = deptNames.substring(0,deptNames.length-1);
        deptIds = deptIds.substring(0,deptIds.length-1);
        callback({"names":deptNames,"ids":deptIds});
    }
</script>